package com.test.daidai.main;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.test.ddaidai.main_project.ProgressAdapter;
import com.test.ddaidai.main_project.PullToRefreshView;
import com.test.ddaidai.main_project.PullToRefreshView.OnFooterRefreshListener;
import com.test.ddaidai.main_project.PullToRefreshView.OnHeaderRefreshListener;
import com.test.ddaidai.main_project.RoundProgressBar;
import com.test.ddaidai.main_project_detail.FinacialProjectDetailActivity;
import com.test.ddaidai_android.activity.R;

public class FinancialManageFragment extends Fragment implements OnHeaderRefreshListener,OnFooterRefreshListener,OnClickListener {

	RoundProgressBar progressBar;
	PullToRefreshView mPullToRefreshView;
	ListView lv;
	List<Integer> list;
	int DATA_MAX = 50 ;
	ProgressAdapter adapter;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);


			List<Integer> result = (List<Integer>) msg.obj;
			switch (msg.what) {
			case PullToRefreshView.PULL_DOWN_STATE:
				mPullToRefreshView.onHeaderRefreshComplete("更新于:"+new Date().toLocaleString());
				//				list.clear();
				if(result.size() == 0){
					Toast.makeText(getActivity(), "当前已是最新数据", Toast.LENGTH_SHORT).show();
				}else{
					list.addAll(result);
				}
				break;
			case PullToRefreshView.PULL_UP_STATE :
				mPullToRefreshView.onFooterRefreshComplete();
				if(result.size() == 0){
					Toast.makeText(getActivity(), "当前已是最新数据", Toast.LENGTH_SHORT).show();
				}else{
					list.addAll(result);
				}
				break;
			}
			adapter.notifyDataSetChanged();
		}

	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		View v = inflater.inflate(R.layout.fragment_financialmanage_main, null);

		list = new ArrayList<Integer>();
		for (int i = 0; i < 4; i++) {
			list.add(i);
		}

		mPullToRefreshView = (PullToRefreshView)v.findViewById(R.id.main_pull_refresh_view);

		adapter = new ProgressAdapter(list, getActivity());
		lv = (ListView) v.findViewById(R.id.lv);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
//				Toast.makeText(getActivity(), "ListView", 1000).show();
				startActivity(new Intent(getActivity(),FinacialProjectDetailActivity.class));
			}

		});
		lv.setAdapter(adapter);
		progressBar=(RoundProgressBar)v.findViewById(R.id.circleProgressbar);

		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		mPullToRefreshView.setLastUpdated(new Date().toLocaleString());
		return v;
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		loadData(PullToRefreshView.PULL_UP_STATE);
		
	}
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {

		loadData(PullToRefreshView.PULL_DOWN_STATE);

	}

	private void loadData(final int what) {
		// 这里模拟从服务器获取数据
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					Thread.sleep(700);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				Message msg = handler.obtainMessage();
				msg.what = what;
				msg.obj = getData();
				handler.sendMessage(msg);
			}
		}).start();
	}


	// 测试数据
	public List<Integer> getData() {
		List<Integer> result = new ArrayList<Integer>();
		Random random = new Random();
		if(list.size() < DATA_MAX){
			for (int i = 0; i < 10; i++) {
				long l = random.nextInt(10000);
				result.add(i);
			}
		}
		return result;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		/*switch (v.getId()) {
		case R.id.:
			
			break;

		default:
			break;
		}*/
	}

}
